﻿Imports System.Data.OleDb

Public Class Comments_LikeManagement
    Inherits ConnectionClass


    Public Function SelectCommentsLikesByCommentId(ByVal CommentId As Int32) As OleDbDataReader
        Try

        
        myConnection.Open()
        Dim SqlString As String = "SELECT Profiles.ProfileName, Profiles.ProfileAvatar, Profiles.ProfileSurname, Profiles.ProfileID FROM   (Comments_Likes INNER JOIN Profiles ON Comments_Likes.UserId = Profiles.UserID) WHERE (Comments_Likes.CommentId = @f1)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, myConnection)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", CommentId)
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        Return dr
        myConnection.Close()
        Catch ex As Exception
            Return Nothing
        End Try
    End Function


    Public Function SelectCountCommentIdUserId(ByVal commentId As Int32, ByVal userId As Int32) As Int32
        Try

        

        myConnection.Open()
        Dim Cmd As OleDbCommand = New OleDbCommand("Select Count(Comment_LikeId) from Comments_Likes where CommentId = @f1 and UserId =@f2 Group By Comment_LikeId", myConnection)
        Cmd.Parameters.AddWithValue("@f1", commentId)
        Cmd.Parameters.AddWithValue("@f2", userId)
        Dim dr = Cmd.ExecuteReader()
        If (dr.HasRows) Then
            dr.Read()
            Dim countLikes = dr.GetValue(0)
            Return countLikes
        Else
            Return 0
        End If

        myConnection.Close()
        Catch ex As Exception
            Return Nothing
        End Try
    End Function


    Public Sub InsertCommentLike(ByVal CommentID As Int32, ByVal UserId As Int32)
        Try


            myConnection.Open()
            Dim SqlString As String = "INSERT INTO Comments_Likes(CommentId,UserId) VALUES(@f1,@f2)"
            Dim newCmd As OleDbCommand = New OleDbCommand(SqlString, myConnection)
            newCmd.CommandType = CommandType.Text
            newCmd.Parameters.AddWithValue("@f1", CommentID)
            newCmd.Parameters.AddWithValue("@f2", UserId)
            newCmd.ExecuteNonQuery()
            myConnection.Close()
        Catch ex As Exception

        End Try
    End Sub

    Public Sub DeleteCommentLikeByCommentIdUserId(ByVal commentId As Int32, ByVal userId As Int32)
        Try

        
        myConnection.Open()
        Dim SqlString As String = "DELETE FROM Comments_Likes where CommentId =@f1 and UserId =@f2"
        Dim newCmd As OleDbCommand = New OleDbCommand(SqlString, myConnection)
        newCmd.CommandType = CommandType.Text
        newCmd.Parameters.AddWithValue("@f1", commentId)
        newCmd.Parameters.AddWithValue("@f2", userId)
        newCmd.ExecuteNonQuery()
        myConnection.Close()
        Catch ex As Exception

        End Try
    End Sub


End Class
